<template>
    <div>
      <form action="">
        <table>
          <tr>
            <td>商品名称</td>
            <td>价格</td>
            <td>操作</td>
          </tr>
          <tr v-for="item in goods">
            <td>{{ item.title }}</td>
            <td>{{ item.price }}</td>
            <td><button @click="add_cart(item.id)">购买</button></td>
          </tr>
        </table>
      </form>
    </div>
</template>

<script>
  import axios from 'axios'
    export default {
        name: "show",
        data(){
          return{
            goods:[],
            name:'',
            price:'',
            id:''
          }
        },
        mounted() {
          axios({
            url:'http://127.0.0.1:8000/show/',
            method:'get',
          }).then(res=>{
            this.goods = res.data.data;
            console.log(res)
          })
        },
        methods:{
          add_cart(id){
            var name = localStorage.getItem('name')

            axios({
              url:'http://127.0.0.1:8000/addcart/',
              method: 'POST',
              data:{'id':id,'name':name}
            }).then(res=>{
              this.$router.push('/showcart')
            })

          }
        }
    }
</script>

<style scoped>

</style>
